package applications.studentApplicationForm.ui.lib;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.objectweb.fractal.fraclet.annotations.Attribute;
import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

@Component(provides = {@Interface(name = "jPanelItf", signature = applications.studentApplicationForm.ui.lib.PanelInterface.class), @Interface(name = "jPanelColAttributes", signature = applications.studentApplicationForm.ui.lib.PanelColInterface.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})
public class FractalCol extends JPanel implements PanelInterface, ontocompo.api.OntoCompoIntrospection, PanelColInterface

{
	
	protected org.objectweb.fractal.api.Component c;


	@Attribute
	protected String semantic_uid;
	
	
	@Attribute
	protected int nbLignes;
	
	@Attribute
	protected int widthCell;
	
	@Attribute
	protected int heightCell;
	
	JLabel [] myJLabelElements ;
	
	protected int count = 0;
	
	
	Dimension d = new Dimension(0,0);
	Dimension d_elements = new Dimension(0,0);
	
		
	@Override
	public void init() {
		setLayout(null);
		d.width = widthCell+getMarge()*2;
		d.height = heightCell*nbLignes+getMarge()*2;
		
		setSize(d);
		setPreferredSize(d);
		setMaximumSize(d);
		setMinimumSize(d);
		
		d_elements.width = widthCell;
		d_elements.height = heightCell;
		
		myJLabelElements = new JLabel[nbLignes];
		
		
		for(int i = nbLignes-1; i >= 0 ; i--)
		{
			myJLabelElements[i] = new JLabel(semantic_uid);
			modifyComponent(i);
			
			super.add(myJLabelElements[i], 0);

		
			System.out.println(" construction de "+semantic_uid+" : "+i);

		}
		
	}
	
	
	public java.awt.Component add(java.awt.Component c) {
		if ((c instanceof JLabel) && (count < nbLignes))
		{
			java.awt.Component result = add(c, count);
			if (result != null) count++;
			
			return result;
		}
		else return null;
	}
	
	@Override
	public java.awt.Component add(java.awt.Component c, int i) {
		if ((c instanceof JLabel) && (i >= 0) && (i < nbLignes))
		{
			remove(myJLabelElements[i]);
			myJLabelElements[i] = (JLabel) c;
			super.add(myJLabelElements[i], i);
			
			modifyComponent(i);
			
			validate();
			return c;
		}
		else return null;
	}
	
	
	private void modifyComponent(int i)
	{
		myJLabelElements[i].setSize(d_elements);
		myJLabelElements[i].setPreferredSize(d_elements);
		myJLabelElements[i].setMaximumSize(d_elements);
		myJLabelElements[i].setMinimumSize(d_elements);
		myJLabelElements[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		
		myJLabelElements[i].setLocation(getMarge(), heightCell*i+getMarge());
		myJLabelElements[i].setBackground(Color.white);
		myJLabelElements[i].setOpaque(true);
	}
	
	public void setText(String msg, int row)
	{
		System.out.println("coucou, pb dans un setText "+msg+" pour la ligne "+(row+1)+" / "+ nbLignes+" / "+semantic_uid);
		if ( (row >= 0) && (row < nbLignes))
		{
			if (myJLabelElements == null) init();
			
			if (myJLabelElements[row] != null) myJLabelElements[row].setText(msg);
			else System.out.println("Erreur... dans un setText "+semantic_uid);
		}

	}


	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}


	@Override
	public void update(Object o) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return c;
	}


	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}


	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return semantic_uid;
	}


	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		semantic_uid =uid;
	}


	@Override
	public int getMarge() {
		// TODO Auto-generated method stub
		return 3;
	}
}
